Hello World is a classic first program in any programming language. We will be making one here to illustrate a few basic points.
First open Visual C# and begin a new Console Application, as shown in the previous section. Name the project HelloWorld. (Step by Step Instructions)
Remember that on the left side of the Visual C# window, you will see your code, which by default is in the Program.cs file. It should look like this:
Now, we are going to add two new lines of code to this. Insert the following two lines of code between the braces ("{...}") after "static void Main(string args[])".
After you insert these lines, it should look like this:
C# is case-sensitive, so capitalization matters in your code.
If you hit the green arrow (the "Run" button) at the top of your screen, a black box with "Hello World" written in it should appear. Remember you can also hit F5 instead of the green arrow.
Once you see the console screen shown above, hit Enter and the program should end.
Congratulations- you have written your very first C# program!
Let's take a closer look at what's happening.
The first thing we see in the code are several lines that start with the using keyword. It's not particularly important right now, but know that these lines tell the program to include several pieces of code that are defined outside of our program. The C# programming language has lots of code stored by default that can be used to make programming easier. For example, the System object is a bunch of code that lets us interact with the computer's input and output (among other things). In order to have access to this pre-written code, we need to include it in our project via the using keyword.
Moving on we see the namespace keyword. Let's ignore this for now; it has to do with keeping our code organized.
Braces ( "{" and "}" ) are used in C# - and most other programming languages - to denote sections of code. Code that falls between a left and right brace is referred to as a block of code. We'll re-visit this concept later.
Next, we see the class keyword. Classes are one of C#'s most important features, and we will talk about this later as well.
The next line is static void Main( string args[] ), which is followed by a block of code (enclosed by { and } ). This is the beginning of the Main function. We'll talk about functions in the next project, but every program you write in C# must have a Main function that begins just like this one. When we run our program, the computer will look for the Main function and start program execution there. In a sense, the Main function is the starting point of every C# program. Anything enclosed by the braces is considered to be part of the Main function.
The first line in our Main function is:
System.Console.WriteLine("Hello World");
By now, you may be able to guess the purpose of this line of code. It tells the computer to print the words "Hello World" to the console. Feel free to change the words in the quotations to whatever you want, and view the output. Remember your text must be in quotation marks.
Also, notice that this line ends with a semi-colon (";"). Every statement in C# must end in a semi-colon. Why didn't the previous lines end in semi-colons, you ask? Because code that denotes the start of a block of code do not need semi-colons. The static void Main( string args[] ) line announces the start of the Main function's code block, so it does not need a semi-colon. This should make more sense as you continue to write programs.
The last important line of code is:
System.Console.ReadLine();
This line tells the program to wait for the user to type something into the console and hit enter; it is waiting for input. That's why the console waited for you to hit the enter key before it closed. If we took that line of code out of our program, what do you think would happen?
Delete the line containing System.Console.ReadLine(), and re-run your program.
You may have noticed the console open and close quickly (displaying "Hello World" briefly). That's because the program reached the end of your code and quit.
That's enough on the HelloWorld program... Let's move on to something more entertaining.
Hello World!
<-- Prev --------- Contents --------- Next -->Hello World is a classic first program in any programming language. We will be making one here to illustrate a few basic points.
First open Visual C# and begin a new Console Application, as shown in the previous section. Name the project HelloWorld. (Step by Step Instructions)
Remember that on the left side of the Visual C# window, you will see your code, which by default is in the Program.cs file. It should look like this:
Now, we are going to add two new lines of code to this. Insert the following two lines of code between the braces ("{...}") after "static void Main(string args[])".
System.Console.WriteLine("Hello World"); System.Console.ReadLine();After you insert these lines, it should look like this:
C# is case-sensitive, so capitalization matters in your code.
If you hit the green arrow (the "Run" button) at the top of your screen, a black box with "Hello World" written in it should appear. Remember you can also hit F5 instead of the green arrow.
Once you see the console screen shown above, hit Enter and the program should end.
Congratulations- you have written your very first C# program!
Let's take a closer look at what's happening.
The first thing we see in the code are several lines that start with the using keyword. It's not particularly important right now, but know that these lines tell the program to include several pieces of code that are defined outside of our program. The C# programming language has lots of code stored by default that can be used to make programming easier. For example, the System object is a bunch of code that lets us interact with the computer's input and output (among other things). In order to have access to this pre-written code, we need to include it in our project via the using keyword.
Moving on we see the namespace keyword. Let's ignore this for now; it has to do with keeping our code organized.
Braces ( "{" and "}" ) are used in C# - and most other programming languages - to denote sections of code. Code that falls between a left and right brace is referred to as a block of code. We'll re-visit this concept later.
Next, we see the class keyword. Classes are one of C#'s most important features, and we will talk about this later as well.
The next line is static void Main( string args[] ), which is followed by a block of code (enclosed by { and } ). This is the beginning of the Main function. We'll talk about functions in the next project, but every program you write in C# must have a Main function that begins just like this one. When we run our program, the computer will look for the Main function and start program execution there. In a sense, the Main function is the starting point of every C# program. Anything enclosed by the braces is considered to be part of the Main function.
The first line in our Main function is:
System.Console.WriteLine("Hello World");By now, you may be able to guess the purpose of this line of code. It tells the computer to print the words "Hello World" to the console. Feel free to change the words in the quotations to whatever you want, and view the output. Remember your text must be in quotation marks.
Also, notice that this line ends with a semi-colon (";"). Every statement in C# must end in a semi-colon. Why didn't the previous lines end in semi-colons, you ask? Because code that denotes the start of a block of code do not need semi-colons. The static void Main( string args[] ) line announces the start of the Main function's code block, so it does not need a semi-colon. This should make more sense as you continue to write programs.
The last important line of code is:
This line tells the program to wait for the user to type something into the console and hit enter; it is waiting for input. That's why the console waited for you to hit the enter key before it closed. If we took that line of code out of our program, what do you think would happen?
Delete the line containing System.Console.ReadLine(), and re-run your program.
You may have noticed the console open and close quickly (displaying "Hello World" briefly). That's because the program reached the end of your code and quit.
That's enough on the HelloWorld program... Let's move on to something more entertaining.
Save and quit.
<-- Prev --------- Contents --------- Next -->